﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_MaintainAwards : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        loadCompet();

        if (!IsPostBack)
        loadData();

        if (Request["AwardID"] != null || Request["PostingID"] != null  || Request["maintain"] != null)
        {
            Control ct;
            ct = Page.LoadControl("~/Share/Insert_Update_AwardIssued.ascx");
            pn_Award_Issued.Controls.Clear();
            pn_Award_Issued.Controls.Add(ct);
        }
    }

    public void loadCompet()
    {
        if (!IsPostBack)
        {
            CompetitionDAO compeDAO = new CompetitionDAO();
            drd_Competitions.DataSource = compeDAO.GetAll();
            drd_Competitions.DataBind();   
        }
        
    }

    public void loadData()
    {
      
        int competitionID = int.Parse(drd_Competitions.SelectedValue.ToString());
        AwardIssuedDAO awardissueDAO = new AwardIssuedDAO();
        DataSet ds = awardissueDAO.Award_Issued(competitionID);
        if (ds.Tables.Count > 0)
        {
            grv_AwardIssued.DataSource = ds;
            grv_AwardIssued.DataBind();
        }
    }

    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grv_AwardIssued.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grv_AwardIssued.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = false;
            }
        }
    }


    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        int competitionID = int.Parse(drd_Competitions.SelectedValue.ToString());
        Response.Redirect("~/Admin/MaintainAwardsIssued.aspx?maintain=Insert&&CompetitionID="+competitionID);
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try{
               AwardIssuedDAO awarDAO = new AwardIssuedDAO();
               AwardIssuedInfo awaInfo = new AwardIssuedInfo();
               bool ischeck = false;

               foreach (GridViewRow row in grv_AwardIssued.Rows)
               {
                   if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                   {
                      // ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true);
                       ischeck = true;
                   }
                  
               }
            
            if(ischeck == true){
            foreach (GridViewRow row in grv_AwardIssued.Rows)
            {
               
                awaInfo.AwardId = int.Parse(grv_AwardIssued.DataKeys[row.RowIndex].Value.ToString());
                awaInfo.PostingId = Int64.Parse(((Label)row.FindControl("lbl_PostID")).Text);
              
                if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                {
                    if (awarDAO.Delete(awaInfo) > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete successfull!');", true);
                    }
                    else
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);
                }
              
            }
            loadData();
            }
            else { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the AwardIssued to delete!');", true); }
        }
       catch (Exception ex) {  }
    
   }
    protected void drd_Competitions_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadData();
    }
  
}